<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入住单号" prop="checkInNo">
        <el-input v-model="queryParams.checkInNo" placeholder="请输入入住单号" clearable @keyup.enter="handleQuery" style="margin-left: 18px;"/>
      </el-form-item>

      <el-form-item label="老人姓名" prop="elderlyName" style="margin-left: 50px;">
        <el-input v-model="queryParams.elderlyName" placeholder="请输入老人姓名" clearable @keyup.enter="handleQuery"  style="margin-left: 18px;"/>
      </el-form-item>

      <el-form-item label="身份证号" prop="elderlyIdCard" style="margin-left: 50px;">
        <el-input v-model="queryParams.elderlyIdCard" placeholder="请输入身份证号" clearable @keyup.enter="handleQuery"  style="margin-left: 18px;"/>
      </el-form-item>

      <br>

      <el-form-item label="入住开始" prop="checkInStartTime">
        <el-date-picker clearable v-model="queryParams.checkInStartTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择入住开始时间"  style="margin-left: 18px;">
        </el-date-picker>
      </el-form-item>
      <el-form-item style="margin-left: 26px;">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <hr style="margin-top: -4px;">
    <div class="apply"><button @click="handleApply">发起入住申请</button></div>
    <el-table v-loading="loading" :data="ruzhuList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center"  width="100" >
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="单据编号" align="center" prop="checkInNo" width="140" />
      <el-table-column label="老人姓名" align="center" prop="elderlyName" />
      <!-- 身份证列：添加脱敏处理 -->
      <el-table-column label="老人身份证" align="center" prop="elderlyIdCard" width="180">
        <template #default="scope">
          <span class="id-card-text">{{ desensitizeIdCard(scope.row.elderlyIdCard) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入住床位" align="center" prop="bedNo" />
      <el-table-column label="入住日期" align="center" prop="checkInStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.checkInStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creatorNickName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleView(scope.row)"
            v-hasPermi="['rutui:ruzhu:edit']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

  </div>
</template>

<script setup name="Ruzhu">
import { listRuzhu, getRuzhu, delRuzhu, addRuzhu, updateRuzhu } from "@/api/rutui/ruzhu"
import { getCurrentInstance, ref, reactive, toRefs } from "vue"
import { useRouter } from 'vue-router'; 
const { proxy } = getCurrentInstance()

const ruzhuList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    checkInNo: null,
    elderlyId: null,
    checkInStartTime: null,
    elderlyName: null,
    elderlyIdCard: null,
  },
  rules: {
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
  }
})
const router = useRouter();
const handleApply = () => {
  router.push("/togetherwork/togetherwork1/checkappeoval");
}
const { queryParams, form, rules } = toRefs(data)
const handleView = (row) => {
  // 跳转到同目录下的 checkdetail.vue，传递当前行的 id（关键参数）
  router.push({
    path: '/rutui/ruzhu/checkdetail',  // 目标页面路径（同目录下）
    query: { 
      id: row.id,  // 传递主键id，用于详情页查询数据
      // 可选：如需传递更多参数，可继续添加
      // checkInNo: row.checkInNo,
      // elderlyName: row.elderlyName
    }
  });
}
// 身份证脱敏函数（保留前6位+后4位，中间用*替换）
function desensitizeIdCard(idCard) {
  if (!idCard || typeof idCard !== 'string' || (idCard.length !== 15 && idCard.length !== 18)) {
    return idCard; // 数据无效时返回原内容，避免异常
  }
  const prefix = idCard.slice(0, 6);
  const suffix = idCard.slice(-4);
  const stars = '*'.repeat(idCard.length - 10); // 适配15/18位身份证
  return `${prefix}${stars}${suffix}`;
}

/** 查询入住管理列表 */
function getList() {
  loading.value = true
  listRuzhu(queryParams.value).then(response => {
    ruzhuList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}


// 表单重置
function reset() {
  form.value = {
    id: null,
    checkInNo: null,
    elderlyId: null,
    checkInStartTime: null,
    checkInEndTime: null,
    creator: null,
    createTime: null,
    nursingLevelId: null,
    bedId: null,
    feeStartDate: null,
    feeEndDate: null,
    deposit: null,
    nursingFee: null,
    bedFee: null,
    otherFee: null,
    medicalInsurancePayment: null,
    governmentSubsidy: null,
    businessId: null,
    extraField1: null,
    extraField2: null,
    elderlyName: null,
    elderlyIdCard: null,
    bedNo: null,
    creatorNickName: null
  }
  proxy.resetForm("ruzhuRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

getList()

</script>

<style scoped>
:deep(.el-table .el-table__row) {
  height: 50px !important;
}

:deep(.el-table .el-table__cell) {
  height: 50px !important;
}

.id-card-text {
  letter-spacing: 1px;
  /* 若文本过长，强制不换行 */
  white-space: nowrap;
}
.apply{
  width: 100%;
  text-align: right;
  padding: 14px 0;
}
.apply button{
  width: 140px;
  height: 32px;
  border-radius: 4px;
  background-color: #409eff;
  color: #fff;
  border: none;
  font-size: 14px;
  transition: background-color 0.5s ease;
}
.apply button:hover{
  background-color: #0168d6;
}
</style>
